#!/bin/bash
# 安装后设置脚本权限
#设置调用名称
PACKAGE="xq-kylin-tools"
chmod 755 /opt/apps/$PACKAGE/$PACKAGE
ln -svf  /opt/apps/$PACKAGE/$PACKAGE  /usr/bin/$PACKAGE
ln -svf  /opt/apps/$PACKAGE/force_delete  /usr/bin/force_delete
ln -svf  /opt/apps/$PACKAGE/cve_kylinos  /usr/bin/cve_kylinos

chmod 644 /usr/share/icons/$PACKAGE.png
chmod 644 /usr/share/applications/$PACKAGE.desktop
chmod 644 /usr/share/icons/hicolor/48x48/apps/$PACKAGE.png
chmod 644 /usr/share/icons/hicolor/512x512/apps/$PACKAGE.png
# 检查依赖
if ! command -v notify-send >/dev/null; then
    echo "安装依赖: libnotify-bin..."
    apt-get install -y libnotify-bin || true
fi

LOGUSER=$(who | awk 'NR==1{print $1}')
[ -z "$LOGUSER" ] && LOGUSER="$SUDO_USER"
HOME="/home/$LOGUSER"
# 检查主目录
if [ ! -d "$HOME" ]; then
    sudo useradd -m "$LOGUSER" >/dev/null 2>&1 || true
    sudo chmod 755 "$HOME"
fi
# 定义可能的桌面路径
DESKTOP_DIR1="$HOME/Desktop"
DESKTOP_DIR2="$HOME/桌面"
# 源.desktop文件路径
DESKTOP_SRC="/usr/share/applications/$PACKAGE.desktop"
# 检查哪个桌面目录存在
if [ -d "$DESKTOP_DIR1" ]; then
    TARGET_DIR="$DESKTOP_DIR1"
elif [ -d "$DESKTOP_DIR2" ]; then
    TARGET_DIR="$DESKTOP_DIR2"
else
    # 如果都不存在，尝试创建默认的Desktop目录
    mkdir -p "$DESKTOP_DIR1"
    TARGET_DIR="$DESKTOP_DIR1"
fi
rm -rf  "$TARGET_DIR/$PACKAGE.desktop"
# 复制.desktop文件到目标桌面目录
cp "$DESKTOP_SRC" "$TARGET_DIR/"
chmod +x "$TARGET_DIR/$PACKAGE.desktop"

# 修改权限为-rwx------ (700)
chmod 700 "$TARGET_DIR/$PACKAGE.desktop"

# 修改文件所有者为当前用户
if [ -n "$LOGUSER" ]; then
    chown "$LOGUSER:$LOGUSER" "$TARGET_DIR/$PACKAGE.desktop"
else
    echo -e "\033[33m警告：无法确定当前用户，跳过修改文件所有者\033[0m" 
fi
# 更新系统数据库
update-mime-database /usr/share/mime || true
update-desktop-database /usr/share/applications || true
gtk-update-icon-cache /usr/share/icons/hicolor || true
# 刷新桌面（可选，适用于某些桌面环境）
if command -v kylin-update-desktop-config >/dev/null 2>&1; then
    kylin-update-desktop-config  # 银河麒麟专用刷新命令
elif command -v update-desktop-database >/dev/null 2>&1; then
    update-desktop-database  # 标准Linux刷新命令
fi
echo -e "\033[34m软件已安装成功，感谢您使用...\033[0m"
